﻿using Core;
using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class FormThemLop : Form
    {
       public static string malop = "-1";
       bool dk = true;
        public FormThemLop()
        {
            InitializeComponent();
        }
        public void SetData()
        {
            if (malop != "-1")
            {
                LopHoc lh = (LopHoc)QuanLyLop.Instance.Find(x => (x as LopHoc).MaLop == malop);
                if (lh != null)
                {
                    tbmalop.Text = lh.MaLop;
                    tbtenlop.Text = lh.TenLop;
                    cbbkhoa.SelectedValue = lh.MaKhoa;
                }
            }
            else
            {
                Reset();
            }
        }
        private void Reset()
        {
            tbmalop.Text = "";
            tbtenlop.Text = "";
            cbbkhoa.SelectedIndex = 0;
        }

        private void FormThemLop_Load(object sender, EventArgs e)
        {
            if (malop != "-1")
            {
                dk = false;
                tbmalop.Enabled = false;
                SetData();
            }
            LoadCombobx();
        }
        private void LoadCombobx()
        {
            List<Khoa> ds = new List<Khoa>();
            foreach (var item in QLDSKhoa.Instance.ListKhoa)
            {
                ds.Add(item as Khoa);
            }
      
                       cbbkhoa.DataSource = ds;
              cbbkhoa.ValueMember = "MaKhoa";
            cbbkhoa.DisplayMember = "TenKhoa";
   
        }
        private LopHoc GetData()
        {
            if (string.IsNullOrWhiteSpace(tbmalop.Text))
            {
                MessageBox.Show("Phải có mã lớp");
                return new LopHoc();
            }
           
                return new LopHoc {
                    MaLop=tbmalop.Text,
                    TenLop=tbtenlop.Text,
                    MaKhoa=Convert.ToInt32 ( cbbkhoa.SelectedValue)
                };
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                LopHoc lh = GetData();
                if(malop=="-1")
                {
          
                QuanLyLop.Instance.Add(lh);
                }
                else   
                { 
                    QuanLyLop.Instance.Update(lh);
                }
                DataSource.Instance.WriteTo(QuanLyLop.Instance.ToString(),"DsLop.txt");
                MessageBox.Show("Thêm Thành Công");
            }
            catch (Exception)
            {

                MessageBox.Show("Thêm thất bại");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbtrue_CheckedChanged(object sender, EventArgs e)
        {
            dk=!dk;
            tbmalop.Enabled = dk;  
        }

        private void btnrs_Click(object sender, EventArgs e)
        {
            Reset();
            malop = "-1";
        }
    }
}
